#![allow(dead_code)]
use crate::domain::CountNum;
use crate::domain::PermissionBean;
use crate::domain::PermissionQueryRequest;
use crate::serviceimpl::init_db;
use crate::utils::string_util::is_digit;
use crate::utils::string_util::right_find_under_line;
use sqlx::Error;
use substring::Substring;

pub async fn activate_all_permissions(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let permissions = &app_state.await.context.permissions;
	let vecter_iterator = ids.iter();
	for permission_id in vecter_iterator {
		let _ = permissions.activate_permission(*permission_id).await;
	}
	Ok(0)
}

pub async fn activate_permission(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.activate_permission(id).await
}

pub async fn add_permission(permission:PermissionBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.add_permission(permission).await
}

pub async fn clone_all_permissions(ids:Vec<i64>) ->Result<u64, Error> {
	let vecter_iterator = ids.iter();
	for permission_id in vecter_iterator {
		let _ = clone_permission(*permission_id).await;
	}
	Ok(0)
}

pub async fn clone_permission(id:i64) ->Result<u64, Error> {
	let mut permission = find_permission_by_id(id).await.unwrap();
	let perm_name = find_permission_indexed_name(permission.perm_name).await.unwrap();
	permission.perm_name = perm_name;
	add_permission(permission).await
}

pub async fn count_search_permissions_by_fields_records(permission_query_request:PermissionQueryRequest) ->Result<CountNum, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.count_search_permissions_by_fields_records(permission_query_request).await
}

pub async fn delete_all_permissions(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let permissions = &app_state.await.context.permissions;
	let vecter_iterator = ids.iter();
	for permission_id in vecter_iterator {
		let _ = permissions.delete_permission(*permission_id).await;
	}
	Ok(0)
}

pub async fn delete_permission(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.delete_permission(id).await
}

pub async fn find_permission_by_id(id:i64) ->Result<PermissionBean, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.find_permission_by_id(id).await
}

pub async fn find_permission_by_perm_name(perm_name:String) ->Result<PermissionBean, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.find_permission_by_perm_name(perm_name).await
}

pub async fn list_active_permissions() ->Result<Vec<PermissionBean>, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.list_active_permissions().await
}

pub async fn list_all_permissions() ->Result<Vec<PermissionBean>, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.list_all_permissions().await
}

pub async fn search_permissions_by_fields(permission_query_request:PermissionQueryRequest) ->Result<Vec<PermissionBean>, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.search_permissions_by_fields(permission_query_request).await
}

pub async fn search_permissions_by_fields_by_page(permission_query_request:PermissionQueryRequest) ->Result<Vec<PermissionBean>, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.search_permissions_by_fields_by_page(permission_query_request).await
}

pub async fn soft_delete_all_permissions(ids:Vec<i64>) ->Result<u64, Error> {
	let app_state = init_db();
	let permissions = &app_state.await.context.permissions;
	let vecter_iterator = ids.iter();
	for permission_id in vecter_iterator {
		let _ = permissions.soft_delete_permission(*permission_id).await;
	}
	Ok(0)
}

pub async fn soft_delete_permission(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.soft_delete_permission(id).await
}

pub async fn toggle_one_permission(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	let permissions = &app_state.await.context.permissions;
	let permission = permissions.find_permission_by_id(id).await.unwrap();
	if permission.active == false {
		let _ = permissions.toggle_permission(id).await;
	} else {
		let count = permissions.count_active_permission_records().await.unwrap();
		if count.count_num > 1 {
			let _ = permissions.toggle_permission(id).await;
		}
	}
	Ok(0)
}

pub async fn toggle_permission(id:i64) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.toggle_permission(id).await
}

pub async fn update_permission(permission:PermissionBean) ->Result<u64, Error> {
	let app_state = init_db();
	app_state.await.context.permissions.update_permission(permission).await
}

pub async fn find_permission_indexed_name(name:String) ->Result<String, Error> {
	let mut short_name = name.clone();
	if name.contains("_") && is_digit(name.substring(right_find_under_line(name.clone())+1,name.chars().count()).to_string()) {
		short_name = name.substring(0,right_find_under_line(name.clone())).to_string();
	}

	let app_state = init_db();
	let permissions = app_state.await.context.permissions.find_permission_indexed_name(short_name.clone()).await.unwrap();

	let mut index_num = 1;
	for permission in permissions {
		let name2 = permission.perm_name;
		if name2.contains("_") && is_digit(name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).to_string()) {
			let index = name2.substring(right_find_under_line(name2.clone())+1,name2.chars().count()).parse::<i32>().unwrap();
			if index >= index_num {
				index_num = index + 1;
			}
		}
	}

	let index_name = short_name.clone() + "_" + &index_num.to_string();
	Ok(index_name)
}

